import 'package:flutter/material.dart';
import 'package:flutter_deer/account/iview/user_bank_iview.dart';
import 'package:flutter_deer/account/models/user_bank_entity.dart';
import 'package:flutter_deer/mvp/base_page_presenter.dart';
import 'package:flutter_deer/net/base_entity.dart';
import 'package:flutter_deer/net/dio_utils.dart';
import 'package:flutter_deer/net/http_api.dart';


class UserBankPagePresenter extends BasePagePresenter<UserBankIMvpView> {

  @override
  void initState() {
  }

  Future<dynamic> fetchUserBankList() async {
    WidgetsBinding.instance.addPostFrameCallback((_) {
      requestNetwork<BaseEntity<List<UserBankEntity>>>(Method.get,
          url: HttpApi.userBankList,
          onSuccess: (data) {
            view.setUserBankList(data?.data);
          },
          onError: (code, msg) {

          }
      );
    });
  }

  Future<dynamic> removeUserBank(String userBankId) async {
     WidgetsBinding.instance.addPostFrameCallback((_) {
      requestNetwork<BaseEntity<List<UserBankEntity>>>(Method.delete,
          url: HttpApi.deleteUserBank.replaceAll("{id}", userBankId.toString()),
          onSuccess: (data) {
            // view.setUserBankList(data!.data);
            view.removeUserBankSuccess();
          },
          onError: (code, msg) {

          }
      );
    });
  }

  Future<dynamic> saveUserBank(String bankId,String cardNo,String cardUsername) async {
    UserBankEntity userBankEntity = UserBankEntity();
    userBankEntity.bankId = bankId;
    userBankEntity.cardNo = cardNo;
    userBankEntity.cardUsername = cardUsername;
    WidgetsBinding.instance.addPostFrameCallback((_) {
      requestNetwork<OperatorBaseEntity>(Method.post,
          url: HttpApi.addUserBank,
          params: userBankEntity.toJson(),
          onSuccess: (data) {
            if(data!.data==true) {
              view.saveUserBankSuccess();
            }else{
              view.saveUserBankFail();
            }
          },
          onError: (code,msg){
            view.saveUserBankFail();
          }
      );
    });
  }

}